#define BLINKER_WIFI
#define BLINKER_MIOT_SENSOR

#define BLINKER_DUEROS_SENSOR

#define BLINKER_ALIGENIE_SENSOR

#define BLINKER_WITHOUT_SSL

#include <Blinker.h>
#include <DHT.h>

char auth[] = "3dcabc27d6ed";
char ssid[] = "Sweet_meng";
char pswd[] = "aita0116.";

BlinkerNumber HUMI("humi");
BlinkerNumber TEMP("temp");

//#define DHTPIN 13
#define DHTPIN 2
#define DHTTYPE DHT11
//#define DHTTYPE DHT22   //(AM2302), AM2321
//#define DHTTYPE DHT21   //(AM2301)
DHT dht(DHTPIN, DHTTYPE);
int32_t humi_read = 0;
float temp_read = 0;

void heartbeat()
{
    HUMI.print(humi_read);
    HUMI.icon("fas fa-tint");
    HUMI.color("#0099FF ");
    TEMP.print(temp_read);
    TEMP.icon("fas fa-thermometer-empty");
    TEMP.color("#FF3300");
}

void miotQuery(int32_t queryCode)
{
    BLINKER_LOG("MIOT Query codes: ", queryCode);
    switch (queryCode)
    {
    case BLINKER_CMD_QUERY_ALL_NUMBER:
        BLINKER_LOG("MIOT Query All");
        BlinkerMIOT.temp(temp_read);
        BlinkerMIOT.humi(humi_read);
        BlinkerMIOT.pm25(20);
        BlinkerMIOT.co2(20);
        BlinkerMIOT.print();
        break;
    case BLINKER_CMD_QUERY_TEMP_NUMBER:
        BLINKER_LOG("MIOT Query TEMP");
        BlinkerMIOT.temp(temp_read);
        BlinkerMIOT.print();
        break;
    case BLINKER_CMD_QUERY_HUMI_NUMBER:
        BLINKER_LOG("MIOT Query HUMI");
        BlinkerMIOT.humi(humi_read);
        BlinkerMIOT.print();
        break;
    case BLINKER_CMD_QUERY_PM25_NUMBER:
        BLINKER_LOG("MIOT Query PM25");
        BlinkerMIOT.pm25(20);
        BlinkerMIOT.print();
        break;
    case BLINKER_CMD_QUERY_TIME_NUMBER:
        BLINKER_LOG("MIOT Query co2");
        BlinkerMIOT.co2(20);
        BlinkerMIOT.print();
        break;
    default:
        BlinkerMIOT.temp(temp_read);
        BlinkerMIOT.humi(humi_read);
        BlinkerMIOT.pm25(20);
        BlinkerMIOT.co2(20);
        BlinkerMIOT.print();
        break;
    }
}

void duerQuery(int32_t queryCode)
{
    BLINKER_LOG("DuerOS Query codes: ", queryCode);
    switch (queryCode)
    {
    case BLINKER_CMD_QUERY_ALL_NUMBER:
        BLINKER_LOG("DuerOS Query All");
        BlinkerDuerOS.aqi(20);
        BlinkerDuerOS.co2(20);
        BlinkerDuerOS.pm10(20);
        BlinkerDuerOS.pm25(20);
        BlinkerDuerOS.humi(humi_read);
        BlinkerDuerOS.temp(temp_read);
        BlinkerDuerOS.time(millis());
        BlinkerDuerOS.print();
        break;
    case BLINKER_CMD_QUERY_AQI_NUMBER:
        BLINKER_LOG("DuerOS Query AQI");
        BlinkerDuerOS.aqi(20);
        BlinkerDuerOS.print();
        break;
    case BLINKER_CMD_QUERY_CO2_NUMBER:
        BLINKER_LOG("DuerOS Query CO2");
        BlinkerDuerOS.co2(20);
        BlinkerDuerOS.print();
        break;
    case BLINKER_CMD_QUERY_PM10_NUMBER:
        BLINKER_LOG("DuerOS Query PM10");
        BlinkerDuerOS.pm10(20);
        BlinkerDuerOS.print();
        break;
    case BLINKER_CMD_QUERY_PM25_NUMBER:
        BLINKER_LOG("DuerOS Query PM25");
        BlinkerDuerOS.pm25(20);
        BlinkerDuerOS.print();
        break;
    case BLINKER_CMD_QUERY_HUMI_NUMBER:
        BLINKER_LOG("DuerOS Query HUMI");
        BlinkerDuerOS.humi(humi_read);
        BlinkerDuerOS.print();
        break;
    case BLINKER_CMD_QUERY_TEMP_NUMBER:
        BLINKER_LOG("DuerOS Query TEMP");
        BlinkerDuerOS.temp(temp_read);
        BlinkerDuerOS.print();
        break;
    case BLINKER_CMD_QUERY_TIME_NUMBER:
        BLINKER_LOG("DuerOS Query time");
        BlinkerDuerOS.time(millis());
        BlinkerDuerOS.print();
        break;
    default:
        BlinkerDuerOS.aqi(20);
        BlinkerDuerOS.co2(20);
        BlinkerDuerOS.pm10(20);
        BlinkerDuerOS.pm25(20);
        BlinkerDuerOS.humi(humi_read);
        BlinkerDuerOS.temp(temp_read);
        BlinkerDuerOS.time(millis());
        BlinkerDuerOS.print();
        break;
    }
}

void aligenieQuery(int32_t queryCode)
{
    BLINKER_LOG("AliGenie Query codes: ", queryCode);
    switch (queryCode)
    {
    case BLINKER_CMD_QUERY_ALL_NUMBER:
        BLINKER_LOG("AliGenie Query All");
        BlinkerAliGenie.temp(temp_read);
        BlinkerAliGenie.humi(humi_read);
        BlinkerAliGenie.pm25(20);
        BlinkerAliGenie.print();
        break;
    case BLINKER_CMD_QUERY_TEMP_NUMBER:
        BLINKER_LOG("AliGenie Query TEMP");
        BlinkerAliGenie.temp(temp_read);
        BlinkerAliGenie.print();
        break;
    case BLINKER_CMD_QUERY_HUMI_NUMBER:
        BLINKER_LOG("AliGenie Query HUMI");
        BlinkerAliGenie.humi(humi_read);
        BlinkerAliGenie.print();
        break;
    case BLINKER_CMD_QUERY_PM25_NUMBER:
        BLINKER_LOG("AliGenie Query PM25");
        BlinkerAliGenie.pm25(20);
        BlinkerAliGenie.print();
        break;
    default:
        BlinkerAliGenie.temp(temp_read);
        BlinkerAliGenie.humi(humi_read);
        BlinkerAliGenie.pm25(20);
        BlinkerAliGenie.print();
        break;
    }
}

void dataRead(const String &data)
{
    BLINKER_LOG("Blinker readString: ", data);
    Blinker.vibrate();
    uint32_t BlinkerTime = millis();
    Blinker.print("millis", BlinkerTime);
}

void setup()
{
    Serial.begin(115200);
    BLINKER_DEBUG.stream(Serial);
    // BLINKER_DEBUG.debugAll();

    dht.begin();
    Blinker.attachHeartbeat(heartbeat);

    Blinker.begin(auth, ssid, pswd);
    Blinker.attachData(dataRead);

    BlinkerMIOT.attachQuery(miotQuery);
    BlinkerDuerOS.attachQuery(duerQuery);
    BlinkerAliGenie.attachQuery(aligenieQuery);
}

void loop()
{
    Blinker.run();
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    if (isnan(h) || isnan(t))
    {
        BLINKER_LOG("Failed to read from DHT sensor!");
    }
    else
    {
        BLINKER_LOG("Humidity: ", h, " %");
        BLINKER_LOG("Temperature: ", t, " *C");
        humi_read = h;
        temp_read = t;
    }
    Blinker.delay(2000);
}
